﻿using System;
using System.Globalization;
using System.Reflection;
using System.Threading;
using System.Windows.Media.Imaging;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.UI;
using YArchitech.HCLoad.Properties;
using YArchitech.HCLoad.Utils;

namespace YArchitech.HCLoad
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	[Journaling(JournalingMode.NoCommandData)]
	public class App : IExternalApplication
	{
		public Result OnStartup(UIControlledApplication application)
		{
			Result result;
			try
			{
				string localLanguage = HcUtils.GetLocalLanguage();
				if (!string.IsNullOrEmpty(localLanguage))
				{
					Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(localLanguage);
				}
				else
				{
                    HcUtils.IsEnglish = ((int)application.ControlledApplication.Language != 6);
					if (!HcUtils.IsEnglish)
					{
						Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("zh-CN");
					}
				}
				this.CreateRibbonHCLoadPanel(application);
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				string hongye_COMPANY = Resources.HONGYE_COMPANY;
				TaskDialog.Show(ex.ToString(), hongye_COMPANY);
				result = Autodesk.Revit.UI.Result.Failed;
			}
			return result;
		}

		public Result OnShutdown(UIControlledApplication application)
		{
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private void CreateRibbonHCLoadPanel(UIControlledApplication application)
		{
			this.m_sPanelName = Resources.HONGYE_PRODUCT;
			application.CreateRibbonTab(this.m_sPanelName);
			this.m_ribbonHcLoadPanel = application.CreateRibbonPanel(this.m_sPanelName, this.m_sPanelName);
			string ribbon_BUTTON_EXPORT_NAME = Resources.RIBBON_BUTTON_EXPORT_NAME;
			string ribbon_BUTTON_IMPORT_NAME = Resources.RIBBON_BUTTON_IMPORT_NAME;
			string ribbon_BUTTON_ANNOTATE_NAME = Resources.RIBBON_BUTTON_ANNOTATE_NAME;
			string ribbon_BUTTON_QUERY_NAME = Resources.RIBBON_BUTTON_QUERY_NAME;
			string ribbon_BUTTON_HELP_NAME = Resources.RIBBON_BUTTON_HELP_NAME;
			string ribbon_BUTTON_HCLOADSP = Resources.RIBBON_BUTTON_HCLOADSP;
			this.AddRibbonButton("Export", ribbon_BUTTON_EXPORT_NAME, "YArchitech.HCLoad.HcExport", "Export32.png", "Export16.png");
			this.m_ribbonHcLoadPanel.AddSeparator();
			this.AddRibbonButton("Import", ribbon_BUTTON_IMPORT_NAME, "YArchitech.HCLoad.HcImport", "Import32.png", "Import16.png");
			this.m_ribbonHcLoadPanel.AddSeparator();
			this.AddRibbonButton("Annotate", ribbon_BUTTON_ANNOTATE_NAME, "YArchitech.HCLoad.HcAnnotate", "Annotate32.png", "Annotate16.png");
			this.m_ribbonHcLoadPanel.AddSeparator();
			this.AddRibbonButton("Query", ribbon_BUTTON_QUERY_NAME, "YArchitech.HCLoad.HcQuery", "Query32.png", "Query16.png");
			this.m_ribbonHcLoadPanel.AddSeparator();
			this.AddRibbonButton("HcHelp", ribbon_BUTTON_HELP_NAME, "YArchitech.HCLoad.HcHelp", "HcHelp32.png", "HcHelp16.png");
			this.m_ribbonHcLoadPanel.AddSeparator();
			this.AddRibbonButton("HCLoadSP", ribbon_BUTTON_HCLOADSP, "YArchitech.HCLoad.HcLoadSP", "YArchitech32.png", "YArchitech16.png");
		}

		private void AddRibbonButton(string sName, string sText, string sClassName, string sUriBigImageFileName, string sUirSmallImageFileName)
		{
			PushButton pushButton = this.m_ribbonHcLoadPanel.AddItem(new PushButtonData(sName, sText, App.m_sModulFullName, sClassName)) as PushButton;
			BitmapImage largeImage = new BitmapImage(new Uri(App.m_sModulPath + "\\res\\" + sUriBigImageFileName));
			pushButton.LargeImage = largeImage;
			BitmapImage image = new BitmapImage(new Uri(App.m_sModulPath + "\\res\\" + sUirSmallImageFileName));
			pushButton.Image = image;
		}

		public RibbonPanel m_ribbonHcLoadPanel;

		private string m_sPanelName;

		public static string m_sModulFullName = Assembly.GetExecutingAssembly().Location;

		public static string m_sModulPath = HcUtils.InstallDirectory;
	}
}
